package com.two.screen.water.balance.ui.activity

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.graphics.Color
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.barlibrary.BarHide
import com.gyf.barlibrary.ImmersionBar
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.BaseBean
import com.two.screen.water.balance.bean.LoginBean
import com.two.screen.water.balance.listener.MyObserver
import com.two.screen.water.balance.net.api.NormalApi
import com.two.screen.water.balance.net.observer.DialogObserver
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.utils.*
import com.two.screen.water.balance.widget.dialog.LoadingDialog
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_forget_pwd.*
import java.util.concurrent.TimeUnit

@Route(path = ARouterUtils.ForgetPwdActivity)
class ForgetPwdActivity : BaseActivity() {
    val TAG = ForgetPwdActivity::class.java.simpleName.plus("=>")
    var loadingDialog: LoadingDialog? = null
    var mLoginDialog: AlertDialog.Builder? = null
    var mChooseBrandDialog: AlertDialog.Builder? = null
    private var mCount = 0
    private var mDisposable: Disposable? = null

    override fun getContentLayout(): Any = R.layout.activity_forget_pwd

    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this)
            .hideBar(BarHide.FLAG_HIDE_BAR)
            .init()
    }

    override fun initData() {
    }


    @SuppressLint("CheckResult")
    override fun initListener() {

        RxView.clicks(mGoBackTv)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribe {
                finish()
            }

        RxView.clicks(mGetMsgTv)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribe {
                getMsg()
            }

        RxView.clicks(mConfirmTv)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribe {
                onConfirmClick()
            }
    }

    private fun onConfirmClick() {

        //TODO:帐号密码
        var mTel = mLoginTelEt?.text.toString()
        var mCode = mCodeEt?.text.toString()
        var mPwd = mNewPwdEt?.text.toString()

        if (mTel.isBlank()) {
            ToastUtil.showToast("手机号不能为空！")
            return
        }

        if (mCode.isBlank()) {
            ToastUtil.showToast("验证码不能为空！")
            return
        }

        if (mPwd.isBlank()) {
            ToastUtil.showToast("密码不能为空！")
            return
        }

        if (mTel == "29658888" && mPwd == "29658888") {
            finish()
        } else {
            NormalApi.getLoginData(sortedMapOf(Params.username to mTel, Params.pwd to CommUtils.encodeByMD5(mPwd), Params.msm to mCode), Urls.forgetPwd)
                .compose(RxUtils.aLifecycle(this@ForgetPwdActivity))
                .subscribe(object : DialogObserver<LoginBean>(this@ForgetPwdActivity, "正在登录...") {
                    override fun onSuccessNext(t: BaseBean<out LoginBean>) {
                       // DataApi.mFutureJson = t.data?.future_json
                        ToastUtil.showToast(t.msg)
                        finish()
                    }

                    override fun dataIsNull(t: BaseBean<out LoginBean>) {
                        onSuccessNext(t)
                        super.dataIsNull(t)
                    }
                })
            //loginSuccess(null, mTel, mPwd)
        }
    }


    private fun getMsg() {
        var mTel = mLoginTelEt?.text.toString()
        if (mTel.isBlank()) {
            ToastUtil.showToast("手机号不能为空！")
            return
        }
        NormalApi.getLoginData(sortedMapOf(Params.username to mTel), Urls.getSMS)
            .compose(RxUtils.aLifecycle(this@ForgetPwdActivity))
            .subscribe(object : DialogObserver<LoginBean>(this@ForgetPwdActivity, "正在获取短信...") {
                override fun onSuccessNext(t: BaseBean<out LoginBean>) {
//                    startCount(
//                        try {
//                            t.data!!.time!!.toLong()
//                        } catch (e: Exception) {
//                            120L
//                        }
//                    )
                }
            })
        startCount()
    }

    private fun startCount(count: Long = 120) {
        //发送成功后，开始计算时间
        Observable.interval(0, 1, TimeUnit.SECONDS) //设置0延迟，每隔一秒发送一条数据
            .take(count + 1)
            .map { t -> count - t }
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxUtils.aLifecycle<Long>(this@ForgetPwdActivity))
            .doOnSubscribe {
                mGetMsgTv.isEnabled = false
                mGetMsgTv.setTextColor(Color.parseColor("#ACACAC"))
            }
            .subscribe(object : MyObserver<Long>() {
                @SuppressLint("SetTextI18n")
                override fun onNext(t: Long) {
                    mGetMsgTv.text = "重新获取${t}"
                }

                override fun onComplete() {
                    mGetMsgTv.isEnabled = true
                    mGetMsgTv.setTextColor(Color.parseColor("#FF7800"))
                    mGetMsgTv.text = "重新发送"
                }
            })
    }


}